/**
 * 
 */
package com.jxc.action.qtmk;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VAnnouncement;
import com.jxc.service.AnnouncementServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 公告相关
 * 
 * @author Tino
 * 
 */
@Namespace("/qtmk")
@Action(value = "announcementAction", results = {
		@Result(name = "tab", location = "/announce/tabAnnounce.jsp"),
		@Result(name = "announce", location = "/announce/announce.jsp"),
		@Result(name = "announceAdd", location = "/announce/announceAdd.jsp"),
		@Result(name = "announceEdit", location = "/announce/announceEdit.jsp") })
public class AnnouncementAction extends BaseAction implements
		ModelDriven<VAnnouncement> {
	private final static Logger logger = Logger
			.getLogger(AnnouncementAction.class);
	@Autowired
	private AnnouncementServiceI announcementService;
	private VAnnouncement model = new VAnnouncement();

	public String noAuth_tabAnnounce() {
		this.getRequest().setAttribute("Announces", announcementService.getAnnouncements(10));
		return "tab";
	}

	@Override
	public VAnnouncement getModel() {
		return model;
	}

	public String announce() {
		getSubAuths("QTMK_GGGL");
		return "announce";
	}

	public void datagrid() {
		DataGrid dataGrid = announcementService.datagrid(model);
		this.writeJson(dataGrid);
	}

	public String announceAdd() {
		return "announceAdd";
	}

	public void add() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
					.getRequest().getSession().getAttribute(
							ResourceUtil.getSessionInfoName());
			model.setUid(sessionInfo.getUserId());
			announcementService.save(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败！");
			ExceptionUtil.getExceptionMessage(e);
		}
		super.writeJson(j);
	}

	public String announceEdit() {
		return "announceEdit";
	}

	public void edit() {
		Json j = new Json();
		try {
			announcementService.update(model);
			j.setMsg("编辑成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("编辑失败！");
			ExceptionUtil.getExceptionMessage(e);
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		announcementService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

}
